const Controller = require('../../core/controller/ApiController');
const {Route, HttpGet, HttpPost, HttpDelete, Middleware, filters} = require('../../../plugin/egg-decorator-router/lib');
const {ExceptionHandler} = require('../../../plugin/egg-controller-exception-handler/lib');

@Route('/api/admin/svcstaff')
class SvcStaffCtrl extends Controller {

    //服务人员搜索
    @HttpGet('/search')
    @ExceptionHandler()
    async searchStaff() {
        const rule = {
            pn: 'int',
            ps: 'int',
            kw: {type: 'string', allowEmpty: true}
        };
        const query = this.ctx.request.query;
        this.validate(rule, query);
        this.result.data = await this.service.admin.staff.main.searchStaff(query.pn, query.ps, query.kw);
        return '查询成功'
    }

    //获取服务人员详情
    @HttpGet('/:id')
    @ExceptionHandler()
    async getStaffDetail() {
        const rule = {
            id: 'int'
        };
        this.validate(rule, this.ctx.params);
        this.result.data = await this.service.admin.staff.main.getStaffDetail(this.ctx.params.id);
        return '查询成功'
    }

    //管理员重置密码
    @HttpPost('/reset/pwd')
    @ExceptionHandler()
    async resetPwd() {
        const rule = {
            id: 'int'
        };
        this.validate(rule, this.ctx.request.query);
        await this.service.admin.staff.main.resetPwd(this.ctx.request.query.id);
        return '重置成功'
    }

    //服务人员详情编辑
    @HttpPost('/edit')
    @ExceptionHandler()
    async editStaffDetail() {
        const rule = {
            svc_staff_id: 'int',
            pic: 'string',
            address: 'string',
            birth: 'int',
            phone: 'string',
            id_card_num: 'string',
            community_list: 'array'
        };
        const body = this.ctx.request.body;
        this.validate(rule, body);
        await this.service.admin.staff.main.editStaffDetail(body);
        return '修改成功'
    }

    //添加服务人员
    @HttpPost('/add')
    @ExceptionHandler()
    async addSvcStaff() {
        const rule = {
            name: 'string',
            sex: 'int',
            pic: 'string',
            address: 'string',
            phone: 'string',
            id_card_num: 'string',
            community_list: 'array'
        };
        this.validate(rule, this.ctx.request.body);
        await this.service.admin.staff.main.addSvcStaff(this.ctx.request.body);
        return '添加成功'
    }

    //批量添加服务人员信息
    @HttpPost('/import')
    @ExceptionHandler()
    async importStaff() {
        const arrRule = {
            svc_staff_list: {
                type: 'array',
                itemType: 'object',
                rule: {
                    name: 'string',
                    sex: 'string',
                    pic: 'string',
                    birth: 'int',
                    address: 'string',
                    phone: 'string',
                    id_card_num: 'string',
                    community_list: 'array'
                }
            }
        };
        this.validate(arrRule, this.ctx.request.body);
        await this.ctx.service.admin.staff.main.importStaff(this.ctx.request.body.svc_staff_list);
        return '添加成功'
    }

    //删除服务人员
    @HttpDelete('/')
    @ExceptionHandler()
    async deleteStaff() {
        const rule = {
            svc_staff_id_list: 'array'
        };
        this.validate(rule, this.ctx.request.body);
        await this.service.admin.staff.main.deleteStaff(this.ctx.request.body.svc_staff_id_list);
        return '删除成功'
    }

    //修改服务人员状态
    @HttpPost('/enable')
    @ExceptionHandler()
    async enableStaff() {
        const rule = {
            svc_staff_id: 'int',
            enable: 'int'
        };
        this.validate(rule, this.ctx.request.body);
        await this.service.admin.staff.main.enableStaff(this.ctx.request.body.svc_staff_id, this.ctx.request.body.enable);
        return '修改成功'
    }
}

module.exports = SvcStaffCtrl;
